package com.tsfot.spider.analyze;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.tsfot.spider.network.NetworkService;
import com.tsfot.spider.task.SpiderTask;
import com.tsfot.spider.task.Task;


/**
 * 解析列表页服务
 * @author frank0
 *
 */
public class ListPageAnalyzeService extends AbstractAnalyzeService{
	
	private final static Logger logger=LoggerFactory.getLogger(ListPageAnalyzeService.class);
	
	private final static int COMPUTER_THREAD_COUNT=10;
	
	private NetworkService networkService;
	
	private LinkedBlockingQueue<Task> pageListQueue = null;
	
	private LinkedBlockingQueue<Task> singlePageQueue = null;
	
	
		
	protected List<Task> doAnalysis(Task task) {
		List<Task> taskList=Lists.newArrayList();
		
		String filePath=task.getLocalPath();
		//读取 进行分析
		String content = fileService.readFileToString(filePath);
		Document doc = Jsoup.parse(content);
		Elements listEle = doc.select(config.getListCssQuery());
		if(listEle!=null){
			Elements aLabel=null;
			Element titleElement=null;
			
			for (int i = 0; i < listEle.size(); i++) {
				Element e=listEle.get(i);
				SpiderTask resultTask=new SpiderTask();
				aLabel = e.select(config.getaLabelCssQuery());
				if (aLabel.size() == 0)
					continue;
				titleElement = aLabel.get(0);
				String url = titleElement.attr("href");
				//TODO 
				resultTask.setUrl(url);
				resultTask.setListIndex(task.getPageNumber()+"_"+i);
				taskList.add(resultTask);
			}
		}
		
		return taskList;
	}

	public void start() {
		//根据本地存储的位置，加载到内存，解析成为详情页，然后下载，生成详情页任务，放入队列
		logger.info("启动列表页分析服务...");
		try {
			ExecutorService fixedThreadPool = Executors.newFixedThreadPool(COMPUTER_THREAD_COUNT);
			while(true){
				Task listPageTask = pageListQueue.take();
				if(listPageTask==Task.NO_TASK){
					break;
				}
				List<Task> singlePageTask=doAnalysis(listPageTask);
				for (Task task : singlePageTask) {
					fixedThreadPool.execute(new ListPageAnalyzeWorker(task));
				}
			}
			fixedThreadPool.shutdown();
			logger.info("列表页分析完成...");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void stop() {
		// TODO Auto-generated method stub
		
	}
	
	private class ListPageAnalyzeWorker implements Runnable {
		
        private Task task;

        ListPageAnalyzeWorker(Task task) {
        	this.task = task;
        }

        public void run() {
        	runInBackground(task);
        }
    }
	
	private void runInBackground(Task task) {
		SpiderTask lpTask=(SpiderTask)task;
		String name=config.getProjectName()+"_list_item_"+lpTask.getListIndex();
		Task singlePageTask= networkService.download(lpTask.getUrl(),name);
		try {
			singlePageQueue.put(singlePageTask);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public void setPageListQueue(LinkedBlockingQueue<Task> pageListQueue) {
		this.pageListQueue = pageListQueue;
	}

	public void setSinglePageQueue(LinkedBlockingQueue<Task> singlePageQueue) {
		this.singlePageQueue = singlePageQueue;
	}

	public NetworkService getNetworkService() {
		return networkService;
	}

	public void setNetworkService(NetworkService networkService) {
		this.networkService = networkService;
	}

}
